#include <iostream>


using namespace std;
using LL=long long;

LL exgcd(LL a,LL b,LL &x,LL &y)
{
	if(!b)
	{
		x=1,y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
int main()
{
	LL a,b,x,y;
	cin>>a>>b;
	LL d=exgcd(a,b,x,y);
	printf("%lld",(x%b+b)%b);
	return 0;
}
